<template>
  <basic-container>
    <el-tabs v-model="activeName" style="margin: 0 10px" @tab-click="handleTabClick">
      <el-tab-pane label="已超期物料" name="overtime"></el-tab-pane>
      <el-tab-pane label="即将超期物料" name="willOvertime"></el-tab-pane>
    </el-tabs>
    <avue-crud
      :option="option"
      :table-loading="loading"
      v-model="form"
      :data="data"
      :page.sync="page"
      :before-open="beforeOpen"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot-scope="{ row }" slot="menu">
        <el-button icon="el-icon-view" size="small" type="text" @click="rowView(row)">详情</el-button>
        <el-button icon="el-icon-view" size="small" type="text" @click="handleGotoInspect(row)">查看待检单</el-button>
      </template>
    </avue-crud>
    <el-dialog
      width="50%"
      top="3vh"
      :fullscreen="false"
      title="详情"
      append-to-body
      custom-class="smtStandDialog"
      v-if="showDetailDialog"
      :modal-append-to-body="true"
      destroy-on-close
      :close-on-click-modal="false"
      :visible.sync="showDetailDialog"
      @close="handleDetailReset"
    >
      <div class="sail-header-dialog-content">
        <avue-crud
          ref="detailCrud"
          :page.sync="detailPage"
          :table-loading="detailDialog.loading"
          :option="detailDialog.tableOption"
          :data="detailDialog.tableData"
          @on-load="getTable"
          @current-change="detailCurrentChange"
          @size-change="detailSizeChange"
        >
        </avue-crud>
      </div>
    </el-dialog>
  </basic-container>
</template>
<script>
import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config/index.vue';
import userConfig from '@/mixins/userConfig';
import { CancelToken } from 'axios';
import { getList, getDetail } from '@/api/warehouse/overTimeExamine';
export default {
  name: 'overTimeExamine', // 会渲染成传入的componentName
  components: {
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      form: {},
      detailQuery: {},
      cancel: null,
      activeName: 'overtime',
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      showDetailDialog: false,
      detailDialog: {
        loading: false,
        tableOption: {
          height: '450px',
          maxHeight: '450px',
          calcHeight: 30,
          tip: false,
          searchShow: false,
          border: true,
          index: true,
          indexLabel: '序号',
          viewBtn: false,
          editBtn: true,
          addBtn: false,
          delBtn: false,
          cellBtn: true,
          labelWidth: 130,
          selection: false,
          refreshBtn: false,
          columnBtn: false,
          highlightCurrentRow: true, // 选中高亮
          dialogClickModal: false,
          menu: false,
          column: [
            {
              label: '包装条码',
              prop: 'sequenceNumber'
            },
            {
              label: '包装数量',
              prop: 'quantity'
            },
            {
              label: '物料批次',
              prop: 'lotNo'
            },
            {
              label: '供应商编码',
              prop: 'vendorCode'
            },
            {
              label: '供应商名称',
              prop: 'vendorName'
            },
            {
              label: '库存中心',
              prop: 'inventoryName'
            },
            {
              label: '储位编码',
              prop: 'areaCode'
            },
            {
              label: '储位名称',
              prop: 'areaName'
            }
          ]
        },
        tableData: []
      },
      detailPage: {
        pageSize: 20,
        currentPage: 1,
        total: 1
      },
      data: [],
      option: {
        calcHeight: 30,
        height: 'calc(100vh - 500px)',
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        addBtn: false,
        indexLabel: '序号',
        columnBtn: false,
        column: [
          {
            label: '物料编码',
            prop: 'partNo',
            search: true
          },
          {
            label: '物料名称',
            prop: 'partName',
            search: true
          },
          {
            label: '物料型号',
            prop: 'partMasterModel'
          },
          {
            label: '物料批次',
            prop: 'lotNo'
          },
          {
            label: '物料数量',
            prop: 'quantity'
          },
          {
            label: '物料保质期天数',
            prop: 'expirationDays'
          },
          {
            label: '生产日期',
            prop: 'productDate'
          },
          {
            label: '过期时间',
            prop: 'overdueDate'
          },
          {
            label: '剩余保质期天数',
            prop: 'remainingDates'
          }
        ]
      }
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ...mapGetters(['permission'])
  },
  methods: {
    rowSave(row, done, loading) {},
    rowUpdate(row, index, done, loading) {},
    rowDel(row) {},
    beforeOpen(done, type) {},
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    handleTabClick() {
      this.handleCancelRequest();
      this.detailQuery = {};
      this.detailPage = {
        pageSize: 20,
        currentPage: 1,
        total: 1
      };
      this.detailDialog.tableData = [];
      this.onLoad(this.page);
    },
    handleCancelRequest() {
      this.cancel('取消请求');
    },
    async onLoad(page, params = {}) {
      this.loading = true;
      try {
        const res = await getList(
          { cancelToken: new CancelToken((c) => (this.cancel = c)) },
          page.currentPage,
          page.pageSize,
          Object.assign(params, this.query, { isOverdue: this.activeName === 'overtime' })
        );
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      } catch (e) {
        console.warn(e);
      } finally {
        this.loading = false;
      }
    },
    handleGotoInspect(row) {
      this.$router.push({
        path: '/quality/overTimeWaitInspect',
        query: {
          partNo: encodeURIComponent(row.partNo)
        }
      });
    },
    rowView(row) {
      this.detailQuery = row;
      this.showDetailDialog = true;
    },
    handleDetailReset() {
      this.showDetailDialog = false;
    },
    detailCurrentChange(currentPage) {
      this.detailPage.currentPage = currentPage;
      // this.getTable(this.detailPage);
    },
    detailSizeChange(pageSize) {
      this.detailPage.pageSize = pageSize;
      //  this.getTable(this.detailPage);
    },
    async getTable(page, params = {}) {
      this.detailDialog.loading = true;
      try {
        const params = {
          current: page.currentPage,
          size: page.pageSize,
          partNo: this.detailQuery.partNo,
          lotNo: this.detailQuery.lotNo,
          productDateStr: this.detailQuery.productDate
        };
        const res = await getDetail(params);
        const data = res.data.data;
        this.detailPage.total = data.total;
        this.detailDialog.tableData = data.records;
      } catch (e) {
        console.warn(e);
      } finally {
        this.detailDialog.loading = false;
      }
    }
  }
};
</script>
<style lang="scss" scoped></style>
